package com.weixin1.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/admin/index/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //禁用浏览器缓存
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires", 0);



        //登录过滤
        if(request.getRequestURI().contains("/admin/index/login.html") || request.getRequestURI().contains("/loginservlet")){
            chain.doFilter(req,resp);
        }else{
            Object manager = request.getSession().getAttribute("manager");
            if(manager!=null){
                chain.doFilter(req,resp);
            }else{
                response.sendRedirect(request.getContextPath()+"/admin/index/login.html");
            }
        }
    }

    @Override
    public void destroy() {

    }
}
